/*
 * This file is part of aion-lightning <aion-lightning.org>
 *
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.player;

import com.aionemu.commons.services.CronService;
import com.aionengine.gameserver.configs.main.CustomConfig;
import com.aionengine.gameserver.model.SellLimit;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.PacketSendUtility;
import javolution.util.FastMap;

/**
 * @author Source
 */
public class PlayerLimitService {

    private static FastMap<Integer, Long> sellLimit = new FastMap<Integer, Long>().shared();

    public static boolean updateSellLimit(Player player, long reward) {
        if (!CustomConfig.LIMITS_ENABLED)
            return true;

        int accoutnId = player.getPlayerAccount().getId();
        Long limit = sellLimit.get(accoutnId);
        if (limit == null) {
            limit = (long) (SellLimit.getSellLimit(player.getPlayerAccount().getMaxPlayerLevel()) * player.getRates().getSellLimitRate());
            sellLimit.put(accoutnId, limit);
        }

        if (limit < reward) {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_DAY_CANNOT_SELL_NPC(limit));
            return false;
        } else {
            limit -= reward;
            sellLimit.putEntry(accoutnId, limit);
            return true;
        }
    }

    public void scheduleUpdate() {
        CronService.getInstance().schedule(new Runnable() {

            @Override
            public void run() {
                sellLimit.clear();
            }

        }, CustomConfig.LIMITS_UPDATE, true);
    }

    public static PlayerLimitService getInstance() {
        return SingletonHolder.instance;
    }

    private static class SingletonHolder {

        protected static final PlayerLimitService instance = new PlayerLimitService();
    }

}
